package com.kevin.zhangchao.animationtest;

import android.animation.TimeInterpolator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BaseInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnStart,btnEnd;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        initView();
    }

    private void initView() {
        btnStart= (Button) findViewById(R.id.btnStart);
        btnEnd= (Button) findViewById(R.id.btnEnd);
        tv= (TextView) findViewById(R.id.tv);
        btnStart.setOnClickListener(this);
        btnEnd.setOnClickListener(this);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btnStart:
//                doAnim();
                MyPointView pointView= (MyPointView) findViewById(R.id.pointview);
                pointView.doPointAnim();
                break;
            case R.id.btnEnd:
                break;
            default:
                break;
        }
    }

    private void doAnim() {
        ValueAnimator animator=ValueAnimator.ofObject(new CharEvaluator(),new Character('A'),new Character('Z'));
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                char curValue=(char)animation.getAnimatedValue();
//                tv.layout(tv.getLeft(),curValue,
//                        tv.getRight(),tv.getHeight()+curValue);
                tv.setText(String.valueOf(curValue));
            }
        });
        animator.setDuration(1000);
        animator.setInterpolator(new MyInterpolator());
//        animator.setEvaluator(new CharEvaluator());
        animator.start();
    }

    class MyInterpolator implements TimeInterpolator{
        @Override
        public float getInterpolation(float input) {
            return input;
        }
    }

    class MyEvaluator implements TypeEvaluator<Integer>{
        @Override
        public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
            return (int)(300+startValue+(endValue-startValue)*fraction);
        }
    }

    class CharEvaluator implements TypeEvaluator<Character>{
        @Override
        public Character evaluate(float fraction, Character startValue, Character endValue) {
            int start=(int)startValue;
            int end=(int)endValue;
            int curValue= (int) (start+(end-start)*fraction);
            char result= (char) curValue;
            return result;
        }
    }
}
